package com.wxservice.serviceimpl;

import com.alibaba.fastjson.JSONObject;
import com.wxservice.domain.request.MessageREQ;
import com.wxservice.domain.request.MessageStatusREQ;
import com.wxservice.domain.request.SentTemplate;
import com.wxservice.domain.request.Template;
import com.wxservice.domain.response.ErrorMsgRES;
import com.wxservice.service.TemplateAndMessageService;
import com.wxservice.util.*;

import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

@Service
public class TemplateAndMessageServiceImpl implements TemplateAndMessageService {


    /**
     * 获取模板列表
     *
     * @param accessToken
     * @return
     */
    public String listTemplate(String accessToken) {
        String result = "failure";
        // 拼接请求地址
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/template/get_all_private_template?access_token=ACCESS_TOKEN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", accessToken);
        // 返回值，调用微信接口
        JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", null);
        if (null != jsonObject && (!jsonObject.containsKey("errcode"))) {
            result = jsonObject.toString();
        }else if(null != jsonObject && jsonObject.containsKey("errcode")){
            result =ErrorUtil.errorConversion(jsonObject);
        }
        return result;
    }

    /**
     * 给入jsonString包含有access_token和template_id
     * 删除模板
     *
     * @param template
     * @return
     */
    public String deleteTemplate(Template template) {
        String result = JSONObject.toJSONString(new ErrorMsgRES(-2,"请输入正确的信息"));
        String jsonString = JSONObject.toJSONString(template);
        JSONObject jsonObject = JSONObject.parseObject(jsonString);
        String access_token = (String) jsonObject.get("access_token");
        String template_id = (String) jsonObject.get("template_id");
        if ((!"".equals(access_token)) && access_token != null && (!"".equals(template_id)) && template_id != null) {
            // 拼接请求地址
            String requestUrl = "https://api.weixin.qq.com/cgi-bin/template/del_private_template?access_token=ACCESS_TOKEN";
            requestUrl = requestUrl.replace("ACCESS_TOKEN", access_token);
            // 返回值
            jsonObject = CommonUtil.httpsRequest(requestUrl, "GET", jsonString);
            if (null != jsonObject && (!jsonObject.containsKey("errcode"))) {
                result = jsonObject.toString();
            }else if(null != jsonObject && jsonObject.containsKey("errcode")){
                result =ErrorUtil.errorConversion(jsonObject);
            }
        }
        return result;
    }

    /**
     * 用于发送模板给单个用户
     *
     * @param sentTemplate
     * @return
     */
    public String sentTemplateToOne(SentTemplate sentTemplate) {
        String jsonString = JSONObject.toJSONString(sentTemplate);
        // 拼接请求地址
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", sentTemplate.getAccess_token());

        // 返回值
        JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "POST", jsonString);
        String result = "failure";
        if (null != jsonObject && (!jsonObject.containsKey("errcode"))) {
            result = jsonObject.toString();
        }else if(null != jsonObject && jsonObject.containsKey("errcode")){
            result =ErrorUtil.errorConversion(jsonObject);
        }
        return result;
    }

    /**
     * 用于发送模板给多个用户
     *
     * @param sentTemplates
     * @return
     */
    @Override
    public String sentTemplateToMany(SentTemplate sentTemplates) {
        SentTemplate sentTemplate = new SentTemplate(sentTemplates.getAccess_token(), sentTemplates.getTemplate_id(), sentTemplates.getData(), sentTemplates.getUrl(), sentTemplates.getMiniprogram());
        List<String> toUsers = sentTemplates.getToManyUser();
        // 拼接请求地址
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/template/send?access_token=ACCESS_TOKEN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", sentTemplates.getAccess_token());
        List<ErrorMsgRES> result=new ArrayList<>();
        String jsonString = "";
        JSONObject jsonObject;

        for (int i = 0; i < toUsers.size(); i++) {
            sentTemplate.setTouser(toUsers.get(i));
            jsonString = JSONObject.toJSONString(sentTemplate);
            jsonObject = CommonUtil.httpsRequest(requestUrl, "POST", jsonString);
            ErrorMsgRES hehe = JSONObject.parseObject(jsonObject.toString(), ErrorMsgRES.class);
            result.add(hehe);
        }
        jsonString = JSONObject.toJSONString(result);
        return jsonString;
    }

    @Override
    public String sentMessageByTag(MessageREQ messageREQ) {
        String jsonString = JSONObject.toJSONString(messageREQ);
        // 拼接请求地址
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/mass/sendall?access_token=ACCESS_TOKEN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", messageREQ.getAccess_token());
        // 返回值
        JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "POST", jsonString);
        String result = "failure";
        if (null != jsonObject && (!jsonObject.containsKey("errcode"))) {
            result = jsonObject.toString();
        }else if(null != jsonObject && jsonObject.containsKey("errcode")){
            result =ErrorUtil.errorConversion(jsonObject);
        }
        return result;
    }

    @Override
    public String sentMessageByOpenID(MessageREQ messageREQ) {
        String jsonString = JSONObject.toJSONString(messageREQ);
        // 拼接请求地址
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/mass/send?access_token=ACCESS_TOKEN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", messageREQ.getAccess_token());
        // 返回值
        JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "POST", jsonString);
        String result = "failure";
        if (null != jsonObject && (!jsonObject.containsKey("errcode"))) {
            result = jsonObject.toString();
        }else if(null != jsonObject && jsonObject.containsKey("errcode")){
            result =ErrorUtil.errorConversion(jsonObject);
        }
        return result;
    }

    @Override
    public String getMessageStatus(MessageStatusREQ messageStatusREQ) {
        String jsonString = JSONObject.toJSONString(messageStatusREQ);
        // 拼接请求地址
        String requestUrl = "https://api.weixin.qq.com/cgi-bin/message/mass/get?access_token=ACCESS_TOKEN";
        requestUrl = requestUrl.replace("ACCESS_TOKEN", messageStatusREQ.getAccess_token());
        // 返回值
        JSONObject jsonObject = CommonUtil.httpsRequest(requestUrl, "POST", jsonString);
        String result = "failure";
        if (null != jsonObject && (!jsonObject.containsKey("errcode"))) {
            result = jsonObject.toString();
        }else if(null != jsonObject && jsonObject.containsKey("errcode")){
            result =ErrorUtil.errorConversion(jsonObject);
        }
        return result;
    }


}
